﻿// -----------------------------------------------------------------------
//  <copyright file="StoreService.Product.cs" company="柳柳软件">
//      Copyright (c) 2014-2015 66SOFT. All rights reserved.
//  </copyright>
//  <site>http://www.66soft.net</site>
//  <last-editor>郭明锋</last-editor>
//  <last-date>2015-12-07 17:57</last-date>
// -----------------------------------------------------------------------

using System;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

using OSharp.Utility;
using OSharp.Utility.Data;

using ZH.JY.Store.Dtos;
using ZH.JY.Store.Models;


namespace ZH.JY.Store
{
    partial class StoreService
    {
        #region Implementation of IStoreContract

        /// <summary>
        /// 获取 产品信息查询数据集
        /// </summary>
        public IQueryable<Product> Products
        {
            get { return ProductRepository.Entities; }
        }

        /// <summary>
        /// 检查产品信息信息是否存在
        /// </summary>
        /// <param name="predicate">检查谓语表达式</param>
        /// <param name="id">更新的产品信息编号</param>
        /// <returns>产品信息是否存在</returns>
        public Task<bool> CheckProductExists(Expression<Func<Product, bool>> predicate, int id = 0)
        {
            predicate.CheckNotNull("predicate");
            return ProductRepository.CheckExistsAsync(predicate, id);
        }

        /// <summary>
        /// 添加产品信息信息
        /// </summary>
        /// <param name="dtos">要添加的产品信息DTO信息</param>
        /// <returns>业务操作结果</returns>
        public Task<OperationResult> CreateProducts(params ProductInputDto[] dtos)
        {
            dtos.CheckNotNull("dtos");
            return ProductRepository.InsertAsync(dtos,
                async dto =>
                {
                    if (await ProductRepository.CheckExistsAsync(m=>m.Name == dto.Name))
                    {
                        throw new Exception($"名称为“{dto.Name}”的商品信息已存在");
                    }
                });
        }

        /// <summary>
        /// 更新产品信息信息
        /// </summary>
        /// <param name="dtos">包含更新信息的产品信息DTO信息</param>
        /// <returns>业务操作结果</returns>
        public Task<OperationResult> UpdateProducts(params ProductInputDto[] dtos)
        {
            dtos.CheckNotNull("dtos" );
            return ProductRepository.UpdateAsync(dtos,
                async (dto, entity) =>
                {
                    if (await ProductRepository.CheckExistsAsync(m => m.Name == dto.Name, dto.Id))
                    {
                        throw new Exception($"名称为“{dto.Name}”的商品信息已存在");
                    }
                });
        }

        /// <summary>
        /// 删除产品信息信息
        /// </summary>
        /// <param name="ids">要删除的产品信息编号</param>
        /// <returns>业务操作结果</returns>
        public Task<OperationResult> DeleteProducts(params int[] ids)
        {
            ids.CheckNotNull("ids" );
            return ProductRepository.DeleteAsync(ids);
        }

        #endregion
    }
}